package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.entity.User;
import com.itheima.exception.BusinessException;
import com.itheima.exception.LoginException;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import com.itheima.template.JWTTemplate;
import com.itheima.template.SMSTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private SMSTemplate smsTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JWTTemplate jwtTemplate;

    @Override
    public String sendSms(String telephone) {
        //1.生成一个随机6位的验证码
        String code = RandomUtil.randomNumbers(6);
        //2.开发时可以节省成本，直接将code打印在控制台上，且存入redis中一份
        redisTemplate.opsForValue().set("SMS_CODE:" + telephone, code, Duration.ofMinutes(5));
        System.out.println(code);
//
//        try {
//            //2.调用模板类的方法发送验证码,接受返回值
//            SendSmsResponse sendSmsResponse = smsTemplate.sendSms(telephone, code);
//
//
//            if ("ok".equalsIgnoreCase(sendSmsResponse.getCode())) {
//                //3.若发送成功,将验证码作为value,手机号作为key存放到redis中且设置时效5分钟
//                redisTemplate.opsForValue().set("SMS_CODE:" + telephone, code, Duration.ofMinutes(5));
//                System.out.println(code);
//            } else {
//                //4.若发送失败,获取错误信息,然后返回
//                throw new BusinessException(sendSmsResponse.getMessage());
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new BusinessException("发送失败,请稍后再试");
//        }

        return code;
    }

    @Override
    public String login(Map<String, String> paramMap) {
        //1.从map中获取用户输入的内容
        String telephone = paramMap.get("telephone");
        String code = paramMap.get("code");

        //2.去redis中查找该手机有无验证码,若无则提示"请先发送验证码"
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object redisCode = valueOperations.get("SMS_CODE:" + telephone);
        if (redisCode == null) {
            throw new BusinessException("请先发送验证码");
        }

        //3.继续判断redis中的验证码和用户输入的验证码是否一致,若不一致则提示"请输入正确验证码"
        if (!redisCode.toString().equals(code)) {
            throw new BusinessException("请输入正确验证码");
        }

        //4.调用mapper根据手机号查找用户,若找不到则提示"手机号未注册"
        User user = userMapper.findByTelephone(telephone);
        if (user == null) {
            throw new BusinessException("手机号未注册");
        }

        //5.登录成功,需要使用jwt技术生成token(不要在token中存放用户的敏感数据),且返回
        user.setPassword(null);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(user);
        return jwtTemplate.createJWT(stringObjectMap);
    }

    @Override
    public void verify(String token) {
        try {
            jwtTemplate.parseJWT(token);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LoginException();
        }
    }
}
